package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.config.ConfigMain;
import com.xforceplus.bsstool.db.NewBssDB;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldBssDB;
import com.xforceplus.bsstool.db.OldUcenterDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep3 {

  /**
   * 获取老运营服务包功能集关联关系列表
   * @return
   */
  public static List<Map> getOldServiceResourceSetRelList(LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_service_resourceset_rel t where t.resourceset_id  ")
            .append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行老运营服务包功能集关联关系获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营服务包功能集关联关系列表
   * @return
   */
  public static Map<String,Map> getNewServiceResourceSetRelMap(LogMain logger) throws Exception {
    Map<String,Map> newServiceResourceSetRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_service_resourceset_rel t where t.resourceset_id  ")
            .append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行新运营服务包功能集关联关系获取，执行SQL: "+sql.toString());
    List<Map> newServiceResourceSetRelList = NewBssDB.getSqlResult(sql.toString());
    for (Map newServiceResourceSetRelMap : newServiceResourceSetRelList){
      newServiceResourceSetRelMapMap.put(String.valueOf(newServiceResourceSetRelMap.get("id")),newServiceResourceSetRelMap);
    }
    return newServiceResourceSetRelMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareServiceResourceSetRelWithNew(List<Map> oldServiceResourceSetRelList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newServiceResourceSetRelMapMap  = getNewServiceResourceSetRelMap(logger);
    for (Map oldServiceResourceSetRelMap : oldServiceResourceSetRelList){
      System.out.println("开始执行服务包功能集关联关系比对 服务包功能集关联关系ID:"+oldServiceResourceSetRelMap.get("id"));
      logger.info("开始执行服务包功能集关联关系比对 服务包功能集关联关系ID:"+oldServiceResourceSetRelMap.get("id"));
      //查询新运营对应功能集
      Map newServiceResourceSetRelMap =newServiceResourceSetRelMapMap.get(String.valueOf(oldServiceResourceSetRelMap.get("id")));
      if (newServiceResourceSetRelMap == null){
        logger.warn("服务包功能集关联关系ID:"+oldServiceResourceSetRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同服务包功能集关联关系ID,继续执行数据信息比对 新运营服务包功能集关联关系："+ JSON.toJSONString(newServiceResourceSetRelMap));
        //新老数据比对： service_package_id resourceset_id
        result = ServiceCommon.compareMapValue(oldServiceResourceSetRelMap,newServiceResourceSetRelMap,
                Lists.newArrayList("service_package_id","resourceset_id"),
                "服务包功能集关联关系ID:"+oldServiceResourceSetRelMap.get("id"),
                logger);
      }

      //重复服务包功能集关联关系检查
      List<Map> sameServiceResourceSetRelList = NewUcenterDB.getSqlResult("select * from bss_service_resourceset_rel where id != "+oldServiceResourceSetRelMap.get("id")
              +" and service_package_id = "+oldServiceResourceSetRelMap.get("service_package_id")
              +" and resourceset_id = "+oldServiceResourceSetRelMap.get("resourceset_id"));
      if (!CollectionUtils.isEmpty(sameServiceResourceSetRelList)){
        logger.error("老运营服务包功能集关联关系ID:"+oldServiceResourceSetRelMap.get("id")+" 新运营存在重复服务包功能集关联关系记录个数："+sameServiceResourceSetRelList.size());
        result = false;
      }
    }
    return result;
  }


  /**
   * 获取老运营服务包列表
   * @return
   */
  public static List<Map> getOldServicePackageList(LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * from bss_service_package t where t.service_package_id ")
            .append("in(select service_package_id from bss_service_resourceset_rel  where resourceset_id ")
            .append(ServiceCommon.getResourceSetIdInSql()).append(")");
    logger.info("执行老运营服务包获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营服务包列表
   * @return
   */
  public static Map<String,Map> getNewServicePackageMap(LogMain logger) throws Exception {
    Map<String,Map> newServicePackageMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * from bss_service_package t where t.service_package_id ")
            .append("in(select service_package_id from bss_service_resourceset_rel  where resourceset_id ")
            .append(ServiceCommon.getResourceSetIdInSql()).append(")");
    logger.info("执行新运营服务包获取，执行SQL: "+sql.toString());
    List<Map> newServicePackageList = NewBssDB.getSqlResult(sql.toString());
    for (Map newServicePackageMap : newServicePackageList){
      newServicePackageMapMap.put(String.valueOf(newServicePackageMap.get("service_package_id")),newServicePackageMap);
    }
    return newServicePackageMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareServicePackageWithNew(List<Map> oldServicePackageList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newServicePackageMapMap  = getNewServicePackageMap(logger);
    for (Map oldServicePackageMap : oldServicePackageList){
      System.out.println("开始执行服务包比对 服务包ID:"+oldServicePackageMap.get("service_package_id"));
      logger.info("开始执行服务包比对 服务包ID:"+oldServicePackageMap.get("service_package_id"));
      //查询新运营对应功能集
      Map newServicePackageMap =newServicePackageMapMap.get(String.valueOf(oldServicePackageMap.get("service_package_id")));
      if (newServicePackageMap == null){
        logger.warn("服务包ID:"+oldServicePackageMap.get("service_package_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同服务包ID,继续执行数据信息比对 新运营服务包："+ JSON.toJSONString(newServicePackageMap));
        //新老数据比对： app_id   service_package_name   service_package_desc  status
        oldServicePackageMap.put("app_id", ConfigMain.APP_ID);
        result = ServiceCommon.compareMapValue(oldServicePackageMap,newServicePackageMap,
                Lists.newArrayList("app_id","service_package_name","service_package_desc","status"),
                "服务包ID:"+oldServicePackageMap.get("service_package_id"),
                logger);
      }

      //重复服务包检查
      List<Map> sameServicePackageList = NewUcenterDB.getSqlResult("select * from bss_service_package where service_package_id != "+oldServicePackageMap.get("service_package_id")
              +" and service_package_name = '"+oldServicePackageMap.get("service_package_name")+"'");
      if (!CollectionUtils.isEmpty(sameServicePackageList)){
        logger.error("老运营服务包ID:"+oldServicePackageMap.get("service_package_id")+" 新运营存在重复服务包(service_package_name重复)记录个数："+sameServicePackageList.size());
        result = false;
      }

    }
    return result;
  }
}
